package com.scau.ums.controller;


import com.scau.ums.mapper.InfoMapper;
import com.scau.ums.mapper.LostandfoundinfoMapper;
import com.scau.ums.model.*;
import com.scau.ums.service.LostAndFoundService;
import com.scau.ums.service.UserInfo;
import com.scau.ums.service.factory.SuggestImpFactory;
import com.scau.ums.service.factory.UserInfoImpFactory;
import com.scau.ums.service.imp.info.InfoImp;
import com.scau.ums.service.imp.password.CommonPasswordImp;
import com.scau.ums.service.imp.repair.EvaluateRepairImp;
import com.scau.ums.service.imp.repair.UserRepairReportImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class UserEntranceController {

    @Autowired
    private UserInfoImpFactory userInfoImpFactory;

    @Autowired
    private UserRepairReportImp userRepairReportImp;

    @Autowired
    private InfoImp infoImp;

    @Autowired
    private SuggestImpFactory suggestImpFactory;

    @Autowired
    private EvaluateRepairImp evaluateRepairImp;

    @Autowired
    private InfoMapper infoMapper;

    @Autowired
    private CommonPasswordImp passwordImp;

    @Autowired
    private LostAndFoundService lostAndFoundService;

    private final int count = 4;


    @RequestMapping(value = "/userEntrance", method = RequestMethod.GET)
    public String toUserEntrance(HttpServletRequest request, @RequestParam(value = "currentPage",required=false,defaultValue="1")Integer currentPage) {

//        if(request.getSession().getAttribute("USER_ID")==null){
//            return "redirect:/";
//        }
        String id= (String) request.getSession().getAttribute("USER_ID");
        int type= (int) request.getSession().getAttribute("USER_TYPE");
        UserInfo userInfo= userInfoImpFactory.getUserInfoImp(type);
        User user=userInfo.getUser(id);
        request.setAttribute("user",user);

        //获取我提交的维修单列表
        List<Repairreport> repairreports=userRepairReportImp.getUncheckedRepairreportByReporterId(0,10,id);
        request.setAttribute("repairreports",repairreports);

        //获取我未评价的维修单列表
        List<Repairreport> unAssessRepairreport=userRepairReportImp.getUnAccessRepairReportByState(0,100,id);
        request.setAttribute("unAssessRepairreport",unAssessRepairreport);

        //获取Info通知信息列表
        List<Info> InfoList=infoImp.getLatestInfoListByReceiverId(id,0,count);
        request.setAttribute("infoList",InfoList);

        //获取我提交的建议列表
        List<Suggestion> suggestionList=suggestImpFactory.getSuggestImp(type).getUserSuggestions(id);
        request.setAttribute("suggestionList",suggestionList);

        //获取我提交的失物招领列表
        String userType = new String();
        if(type == User.STUDENT)
            userType = "学生";
        else if(type == User.TEACHER)
            userType = "教师";

        List<Lostandfoundinfo> lostandfoundinfoList = lostAndFoundService.getLostAndFoundById(id,userType);
        request.setAttribute("lostandfoundinfoList",lostandfoundinfoList);

        return "forward:/WEB-INF/jsp/userEntrance.jsp";
    }


//    @RequestMapping(value="/userEntrance",method = RequestMethod.POST)
//    public void userEntrance(HttpServletRequest request){
//        //评价报修单=>插入数据库
//        String evaluation=request.getParameter("choice1");
//        String remark=request.getParameter("remark");
//        int reportId=Integer.parseInt(request.getParameter("reportId"));
//        System.out.println(evaluation);
//        System.out.println("==========================uu========");
//        System.out.println(remark);
//        System.out.println(evaluateRepairImp.evaluateRepair(reportId,evaluation,remark));
//
//    }
    /**
     *
     * part:利用ajax动态获取通知列表
     * exitor:Superior
     * count为每次读取的通知行数
     */


    @RequestMapping("/showInfo")
    @ResponseBody

    public List<Info> showInfo(ModelMap map,HttpServletRequest request,@RequestParam("clickNum") int clickNum){
        String id= (String) request.getSession().getAttribute("USER_ID");
        //查询数据库所有通知信息
//        List<Info> InfoList=infoImp.getLatestInfoList();
//        int clickNum=Integer.parseInt(request.getParameter("clickNum"));
        int start;
        if(clickNum==0){
            start=0;
        }else {
            start = clickNum * count;
        }

//        System.out.println(clickNum);

        List<Info> InfoList=infoImp.getLatestInfoListByReceiverId(id,start,count);


        //map.addAttribute("lostInfoList",mapper.SelectAllLostInfo());

        return InfoList;//返回json，由UserEntrance.jsp处理
    }

    //评价报修单ajax
    @RequestMapping("/evaluateRepairreport")
    @ResponseBody

    public String evaluateRepairreport(HttpServletRequest request,@RequestParam("reportId") int reportId){

        String evaluation=request.getParameter("evaluation");
        String remark=request.getParameter("remark");
        reportId=Integer.parseInt(request.getParameter("reportId"));
        evaluateRepairImp.evaluateRepair(reportId,evaluation,remark);


        //map.addAttribute("lostInfoList",mapper.SelectAllLostInfo());

        return evaluation;//返回json，由UserEntrance.jsp处理
    }

    //查看通知的ajax
    @RequestMapping("/transferState")
    @ResponseBody

    public Info transferState(HttpServletRequest request,@RequestParam("infoId") int infoId){

        Info info=infoMapper.selectByPrimaryKey(infoId);
        String state=info.getState();
        if(state.equals("unchecked")){
            info.setState("checked");
        }else{
            System.out.println("不做任何改变");
        }
        infoMapper.updateByPrimaryKey(info);

//        System.out.println("====================="+info.getState());

        return info;//返回json，由UserEntrance.jsp处理
    }

    //接收通知的ajax
    @RequestMapping("/receiveInfo")
    @ResponseBody

    public String receiveInfo(HttpServletRequest request,@RequestParam(value="data",defaultValue="0") String data){


        System.out.println(data+"=====================userEntrance已经获取");

        return data;//返回json，由UserEntrance.jsp处理
    }

    //历史记录的ajax
    @RequestMapping("/history")
    @ResponseBody

    public List<Repairreport> history(HttpServletRequest request,@RequestParam("clickNum1") int clickNum1){

        String id=(String)request.getSession().getAttribute("USER_ID");

        int start;
        int cou=8;
        if(clickNum1==1){
            start=0;
        }else {
            start = (clickNum1-1) * cou;
        }
        List<Repairreport> AllRepairreport=userRepairReportImp.getAllRepairReportById(start,cou,id);
        System.out.println("==========="+id);

        return AllRepairreport;//返回json，由UserEntrance.jsp处理
    }

    //删除历史记录的ajax
    @RequestMapping("/delHistory")
    @ResponseBody

    public int delHistory(HttpServletRequest request,@RequestParam("deletedId") int deletedId){

        return userRepairReportImp.deleteHistoryRepairReportById(deletedId);

    }
}
